/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.genew.squadra.xml.model.builder;
import java.util.Date;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.genew.squadra.xml.model.content.*;
import org.genew.squadra.xml.model.content.guide.Guide;
import org.genew.squadra.xml.model.content.guide.Reference;
import org.genew.squadra.xml.model.content.manifest.Item;
import org.genew.squadra.xml.model.content.manifest.Manifest;
import org.genew.squadra.xml.model.content.metadati.Metadata;
import org.genew.squadra.xml.model.content.spine.ItemRef;
import org.genew.squadra.xml.model.content.spine.Spine;
/**
 *
 * @author Matteo
 */
public class ContentBuilder implements Builder{
    
    
    public static void buildOpf(Content ct, XMLStreamWriter xmlv) throws XMLStreamException{
        xmlv.writeStartDocument("UTF-8","1.0");
        xmlv.writeStartElement("package");
        xmlv.writeAttribute("unique-identifier", ct.getMetadata().getIdentifier().getValue()); //Il secondo valore deve diventare dinamico
        xmlv.writeAttribute("version", "3.0");
        //xmlv.writeAttribute("prefix", "rendition http://wwww.idpf.org/2007/opf");
        xmlv.writeAttribute("xmlns", "http://www.idpf.org/2007/opf");
        buildMetadata(ct.getMetadata(), xmlv);
        buildManifest(ct.getManifest(), xmlv);
        buildSpine(ct.getSpine(), xmlv);
        builGuide(ct.getGuide(), xmlv);
        xmlv.writeEndElement();
        xmlv.writeEndDocument();
    }
    
    private static void buildManifest(Manifest mnf, XMLStreamWriter xmlv) throws XMLStreamException{
        int iteratore = 0;
        xmlv.writeStartElement("manifest");
        /*Controlliiiii*/
            for(iteratore = 0; iteratore < mnf.getItems().size(); iteratore++){
                xmlv.writeStartElement("item");
                Item temp = mnf.getItems().get(iteratore);
                //xmlv.writeAttribute("fallback", temp.getFallback());
                xmlv.writeAttribute("id", temp.getId());
                xmlv.writeAttribute("href", temp.getHref());
                if (temp.getId().equals("toc")){
                    if (temp.getProprierties() != null) {
                    xmlv.writeAttribute("properties", temp.getProprierties());
                    }
                    
                }else {
                    if(iteratore == 1) xmlv.writeAttribute("fallback", "ch01");
                    }
                
                xmlv.writeAttribute("media-type", temp.getMedia_type());
                xmlv.writeEndElement();
            }
       
        xmlv.writeEndElement();   
    }
    
    private static void buildMetadata(Metadata mtd, XMLStreamWriter xmlv) throws XMLStreamException{
        /*Metadata Attenzione a questi
            xmlns:opf="http://www.idpf.org/2007/opf"
            xmlns:dc="http://purl.org/dc/elements/1.1/"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:dcterms="http://purl.org/dc/terms/"
         */
        xmlv.writeStartElement("metadata");
        xmlv.writeAttribute("xmlns:dc", "http://purl.org/dc/elements/1.1/");    
        
        /*CONTROLLO SU I CAMPI IMMESSI E NON */
        	xmlv.writeStartElement("dc:identifier");
            xmlv.writeAttribute("id", mtd.getIdentifier().getValue());
            xmlv.writeCharacters("urn:usbn:" + mtd.getIdentifier().getValue());
            xmlv.writeEndElement();
            
            xmlv.writeStartElement("dc:title");
//            xmlv.writeAttribute("id", mtd.getTitle().getId());
            xmlv.writeCharacters(mtd.getTitle().getValue());
            xmlv.writeEndElement();
                
            xmlv.writeStartElement("dc:creator");
//            xmlv.writeAttribute("id", mtd.getCreator().getId());
            xmlv.writeCharacters(mtd.getCreator().getValue());
            xmlv.writeEndElement();

            xmlv.writeStartElement("dc:language");
//            xmlv.writeAttribute("id", mtd.getLanguage().getId());
            xmlv.writeCharacters(mtd.getLanguage().getValue());
            xmlv.writeEndElement();

            xmlv.writeStartElement("dc:publisher");
//          xmlv.writeAttribute("id", mtd.getLanguage().getId());
            xmlv.writeCharacters(mtd.getPublisher().getValue());
          	xmlv.writeEndElement();
          	
            xmlv.writeStartElement("dc:rights");
//            xmlv.writeAttribute("id", mtd.getLanguage().getId());
            xmlv.writeCharacters(mtd.getRights().getValue());
            xmlv.writeEndElement();
            
            xmlv.writeStartElement("dc:format");
            xmlv.writeCharacters(mtd.getFormat().getValue());
            xmlv.writeEndElement();
            
            xmlv.writeStartElement("dc:source");
            xmlv.writeCharacters(mtd.getSource().getValue());
            xmlv.writeEndElement();
            
            xmlv.writeStartElement("dc:type");
            xmlv.writeCharacters(mtd.getType().getValue());
            xmlv.writeEndElement();

            xmlv.writeStartElement("dc:date");
            xmlv.writeCharacters(mtd.getDate().getValue().toString());
            xmlv.writeEndElement();

            xmlv.writeStartElement("dc:contributor");
            xmlv.writeCharacters(mtd.getContributor().getValue());
            xmlv.writeEndElement();

            xmlv.writeStartElement("dc:relation");
            xmlv.writeCharacters(mtd.getRelation().getValue());
            xmlv.writeEndElement();

            xmlv.writeStartElement("dc:description");
            xmlv.writeCharacters(mtd.getDescription().getValue());
            xmlv.writeEndElement();

            xmlv.writeStartElement("dc:coverage");
            xmlv.writeCharacters(mtd.getCoverage().getValue());
            xmlv.writeEndElement();            

            xmlv.writeStartElement("dc:subject");
            xmlv.writeCharacters(mtd.getSubject().getValue());
            xmlv.writeEndElement();

            xmlv.writeStartElement("dc:relation");
            xmlv.writeCharacters(mtd.getRelation().getValue());
            xmlv.writeEndElement();
            
            //<meta property="dcterms:modified">2011-06-30T21:37:00Z</meta>

            xmlv.writeStartElement("meta");
            xmlv.writeAttribute("property", "dcterms:modified");
            
            String d = "2011-06-30T21:37:00Z";
            xmlv.writeCharacters(d);
            xmlv.writeEndElement();
            
        xmlv.writeEndElement();
    }
    
    private static void buildSpine(Spine sp, XMLStreamWriter xmlv) throws XMLStreamException{
        int iteratore = 0;
        xmlv.writeStartElement("spine");
        xmlv.writeAttribute("toc", sp.getToc());
            xmlv.writeAttribute("id", sp.getId());
//            xmlv.writeAttribute("page-progression-direction", sp.getPageProgDir());
            for(iteratore = 0; iteratore < sp.getItemRef().size(); iteratore++){
            xmlv.writeStartElement("itemref");
            ItemRef temp = sp.getItemRef().get(iteratore);
            xmlv.writeAttribute("idref", temp.getIdRef());
            String linear = new String();
            
            if (temp.getLinear().equals(true)) linear = "yes"; else linear = "no";
            xmlv.writeAttribute("linear", linear);
            //xmlv.writeAttribute("id", temp.getXmlId()); //prima era xmlID
            //xmlv.writeAttribute("properties", temp.getProperties());
            xmlv.writeEndElement();
            }
            xmlv.writeEndElement();
    }
    private static void builGuide(Guide gd, XMLStreamWriter xmlv) throws XMLStreamException{
        int iteratore = 0;
        
        if(gd.getReferences().size() !=0){
        xmlv.writeStartElement("guide");
        }
            for(iteratore = 0; iteratore < gd.getReferences().size(); iteratore++){
              Reference temp = gd.getReferences().get(iteratore);
                xmlv.writeStartElement("reference");
                xmlv.writeAttribute("type", temp.getType());
                xmlv.writeAttribute("title", temp.getTitle());
                xmlv.writeAttribute("href", temp.getHref());
                xmlv.writeEndElement();
            }
            
            /**/
            if(gd.getReferences().size() !=0){
            	xmlv.writeEndElement();
            }
            

    }




}
